{


}


var

  byte sdaPin, sclPin

pub StartI2C(_sda, _scl)

  sdaPin := _sda
  sclPin := _scl

  dira[sdaPin]~
  dira[sclPin]~
  outa[sdaPin]~
  outa[sclPin]~

pub TxByte(byteToSend) | i

  'byteToSend &= $FE

  repeat i from 7 to 0
    SclLow
    if (byteToSend >> i) & 1
      SdaHigh
    else
      SdaLow
    SclHigh

  SclLow
  dira[sdaPin]~
  SclHigh
  result := ina[sdaPin]
  'SclLow

pub RxByte(inAck) | i

  result~
  'SclHigh
  dira[sdaPin]~
  repeat i from 7 to 0
    SclLow
    SclHigh
    result |= ina[sdaPin] << i

  'repeat 500
  
  SclLow
  ifnot inAck & 1
    SdaHigh
  else
    SdaLow
  sclLow
  sclHigh
      
  
pub SendStart

  SclHigh
  SdaHigh
  SdaLow

pub SendStop

  'SclLow
  'SdaLow
  SclHigh
  
  SclLow
  SdaLow
  SclHigh
  SdaHigh
  

pri SclHigh

  dira[sclPin]~

pri SclLow

  dira[sclPin]~~

pri SdaHigh

  dira[sdaPin]~

pri SdaLow

  dira[sdaPin]~~